<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.File" %>
<%
    File[] fileList = (File[]) request.getAttribute("fileList");
    String message = (String) session.getAttribute("message");
    session.removeAttribute("message");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文件列表 - 文件上传系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 25px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.2em;
            margin-bottom: 10px;
        }

        .content {
            padding: 30px;
        }

        .message {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 8px;
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .file-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .file-table th, .file-table td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }

        .file-table th {
            background: #f8f9fa;
            font-weight: bold;
            color: #495057;
        }

        .file-table tr:hover {
            background: #f8f9ff;
        }

        .action-btn {
            padding: 8px 15px;
            margin: 0 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 0.9em;
        }

        .download-btn {
            background: #28a745;
            color: white;
        }

        .delete-btn {
            background: #dc3545;
            color: white;
        }

        .empty-state {
            text-align: center;
            padding: 50px;
            color: #6c757d;
        }

        .empty-state i {
            font-size: 3em;
            margin-bottom: 20px;
        }

        .nav-links {
            text-align: center;
            margin-top: 30px;
        }

        .nav-link {
            display: inline-block;
            margin: 0 10px;
            padding: 12px 25px;
            background: #6c757d;
            color: white;
            text-decoration: none;
            border-radius: 25px;
            transition: background 0.3s ease;
        }

        .nav-link:hover {
            background: #5a6268;
        }

        .nav-link.primary {
            background: #007bff;
        }

        .nav-link.primary:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>📂 文件管理</h1>
        <p>查看和管理所有已上传的文件</p>
    </div>

    <div class="content">
        <% if (message != null) { %>
        <div class="message">
            <%= message %>
        </div>
        <% } %>

        <% if (fileList.length == 0) { %>
        <div class="empty-state">
            <div>📭</div>
            <h3>暂无文件</h3>
            <p>还没有上传任何文件，快去上传第一个文件吧！</p>
        </div>
        <% } else { %>
        <table class="file-table">
            <thead>
            <tr>
                <th>文件名</th>
                <th>大小</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <% for (File file : fileList) {
                if (file.isFile()) {
            %>
            <tr>
                <td><%= file.getName() %></td>
                <td><%= String.format("%,d", file.length()) %> bytes</td>
                <td>
                    <a href="upload?action=download&file=<%= file.getName() %>"
                       class="action-btn download-btn">📥 下载</a>
                    <a href="upload?action=delete&file=<%= file.getName() %>"
                       class="action-btn delete-btn"
                       onclick="return confirm('确定要删除文件 <%= file.getName() %> 吗？')">🗑️ 删除</a>
                </td>
            </tr>
            <% }
            } %>
            </tbody>
        </table>
        <p>共找到 <%= fileList.length %> 个文件</p>
        <% } %>

        <div class="nav-links">
            <a href="upload" class="nav-link primary">📤 上传新文件</a>
            <a href="upload?action=list" class="nav-link">🔄 刷新列表</a>
        </div>
    </div>
</div>
</body>
</html>